package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.domain.iam.entity.GroupComponent;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.GroupComponentCreate;
import com.harmontronics.hanma.paas.paas.domain.iam.service.HmUserGroupService;
import com.harmontronics.hanma.paas.paas.service.HmUserGroupReportAppService;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmUserGroupComponentAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmUserGroupComponentCreateInput;


/**
 * 用户组可见/可管理范围模块
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-27 11:08:16
 */
@RestController
@RequestMapping("/api/hmusergroupcomponent")
@Api(tags = {"用户组可见/可管理范围模块"})
public class HmUserGroupComponentController {
    @Autowired
    private HmUserGroupComponentAppService hmUserGroupComponentappService;

    @Autowired
    private HmUserGroupReportAppService hmUserGroupReportAppService;

    @GetMapping("/select-list")
    @ApiOperation(value = "获取所有的的组件")
    public ResponseEntity<List<GroupComponent>> get(@RequestParam String type, @RequestParam Long groupId) {
        return ResponseEntity.ok(hmUserGroupComponentappService.getByComponentType(type, groupId));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有已经选择的组件")
    public ResponseEntity<List<GroupComponent>> list(@RequestParam Integer type, @RequestParam Long userGroupId) {
        return ResponseEntity.ok(hmUserGroupComponentappService.selectByGroupComponentType(type, userGroupId));
    }


    @PostMapping
    @ApiOperation(value = "创建新的引用")
    public ResponseEntity create(@RequestBody HmUserGroupComponentCreateInput groupComponentCreate) {
        GroupComponentCreate componentCreate = BeanUtil.toBean(groupComponentCreate,GroupComponentCreate.class);
        hmUserGroupComponentappService.create(componentCreate);
        return ResponseEntity.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除对应的组件")
    public ResponseEntity delete(@RequestParam Long id) {
        // 组件内的数据也需要删除
        hmUserGroupReportAppService.deleteWithGroupComponentId(id);
        hmUserGroupComponentappService.removeById(id);
        return ResponseEntity.ok();
    }

}
